package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.ZchukuMapper;
import cn.gson.fengboot.model.mapper.ZorderMapper;
import cn.gson.fengboot.model.mapper.ZreturnedgoodMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ZreturnedgoodService {
    @Autowired
    ZreturnedgoodMapper ddmapper;

    @Autowired
    ZreturnedgoodDao dao;

    @Autowired
    ZreturnedgooddetailDao dao2;

    @Autowired
    ZorderdetailsDao ddxqdao;

    @Autowired
    ZorderDao dddao;

    @Autowired
    ZorderMapper ddxqmapper;

    @Autowired
    ZchukuxqDao ckxqdao;

    @Autowired
    ZchukuDao ckdao;

    @Autowired
    ZchukuMapper ckmapper;
    /*
        退货单弹出框查询方法查询方法
     */
    public List<Zreturnedgood> ddlist(String search, String search2){
        return ddmapper.ddlist(search,search2);
    }

    /*
        退货单列表查询方法
    */
    public List<Zreturnedgood> ddlist2(String search, String search2){
        return ddmapper.ddlist2(search,search2);
    }

    /*
        订单详情退货单
    */
    public List<Zreturnedgood> ddthd(String search){
        return ddmapper.ddthd(search);
    }

    /*
     订单详情退货单
    */
    public List<Zreturnedgooddetail> thdxq(String search){
        return ddmapper.kaipiao2(search);
    }

    /*
        退货单弹框
    */
    public List<Zreturnedgood> thtk(String search,String search2){
        return ddmapper.thtk(search,search2);
    }


    /*
        退货单新增方法
    */
    public void xz(Zreturnedgood thd){
        //新增订单表
        Zreturnedgood save = dao.save(thd);
        System.out.println(save.getReturgoodId());
        //新增采购详情表
        thd.getThxq().forEach(pur->{
            pur.setReturgoodId(save);
        });
        dao2.saveAll(thd.getThxq());

        Zorder zorder = dddao.findById(thd.getOrderId().getOrderId()).get();
        zorder.setOrderReturn("是");
        dddao.save(zorder);

        //修改订单详情数量
        List<Zorderdetails> ddxqlist = ddxqmapper.ddxqlist(zorder.getOrderId(),123);
        for (int i =0;i<ddxqlist.size();i++) {
            for (int j =0;j<ddxqlist.size();j++) {
                if(ddxqlist.get(i).getProdetailId().getProdetailId()==thd.getThxq().get(j).getProdetailId().getProdetailId()){
                    ddxqlist.get(i).setOrderdetailRealitynum(ddxqlist.get(i).getOrderdetailRealitynum()-thd.getThxq().get(j).getRegodetailNum());
                }
            }
        }
        ddxqdao.saveAll(ddxqlist);

        //修改出库详情数量
        List<Zchukuxq> chukuxq = ckmapper.chukuxq(thd.getChukuId().getChukuId(),123);
        for (int i =0;i<chukuxq.size();i++) {
            for (int j =0;j<thd.getThxq().size();j++) {
                if(chukuxq.get(i).getProdetailId().getProdetailId()==thd.getThxq().get(j).getProdetailId().getProdetailId()){
                    chukuxq.get(i).setChukuKetueinumber(chukuxq.get(i).getChukuKetueinumber()-thd.getThxq().get(j).getRegodetailNum());
                }
            }
        }
        ckxqdao.saveAll(chukuxq);

        Integer sfth = 0;
            for (int i =0;i<chukuxq.size();i++) {
                if(chukuxq.get(i).getChukuKetueinumber()!=0){
                    sfth=1;
                }
            }

        Zchuku zchuku = ckdao.findById(thd.getChukuId().getChukuId()).get();
        if (sfth==0){
            zchuku.setChukuShifouth("已退换");
        }else {
            zchuku.setChukuShifouth("部分退换");
        }

        ckdao.save(zchuku);
    }

    //谭方圆=>客户合并查询退货
    public List<Zreturnedgood> tfytuihuolists(Integer id1,Integer id2){
        return ddmapper.tfytuihuolists(id1, id2);
    }
}
